package com.jubaozan.service.platform.decorator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.dao.youme.domain.Company;
import com.jubaozan.dao.youme.domain.Idcard;
import com.jubaozan.dao.youme.domain.Store;
import com.jubaozan.dao.youme.domain.StoreAudit;
import com.jubaozan.dao.youme.service.CompanyService;
import com.jubaozan.dao.youme.service.IdcardService;
import com.jubaozan.dao.youme.service.StoreAuditService;
import com.jubaozan.dao.youme.service.StoreService;
import com.jubaozan.service.platform.constants.PlatformStatusEnum;
import com.jubaozan.service.platform.constants.PlatformTypeEnum;
import com.jubaozan.service.platform.exception.PlatformException;
import com.jubaozan.service.platform.vo.PlatformDetailsVO;
import com.jubaozan.service.platform.vo.PlatformSearchVO;
import com.jubaozan.service.platform.vo.StoreGoodsVO;
import com.jubaozan.service.platform.vo.StoreListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PlatformAuditDecorator
 * @Description 个体平台信息查询Decorator
 * @Author SunWei
 * @Version 1.0
 **/
@Slf4j
@Component
public class PlatformMessageDecorator {

    private final CompanyService companyService;

    private final IdcardService idcardService;

    private final StoreService storeService;

    private final StoreAuditService storeAuditService;


    public PlatformMessageDecorator(CompanyService companyService, IdcardService idcardService, StoreService storeService, StoreAuditService storeAuditService) {
        this.companyService = companyService;
        this.idcardService = idcardService;
        this.storeService = storeService;
        this.storeAuditService = storeAuditService;
    }

    /**
     * 个体平台信息列表
     *
     * @param platformSearchVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    public  IPage<StoreListVO> query(PlatformSearchVO platformSearchVO, Integer pageNo, Integer pageSize) {
        List<StoreListVO> storeListList = new ArrayList<>();
        IPage<StoreListVO> voiPage = new Page<>();
        //分页
        Page<Store> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        //手机号码
        if (ObjectUtil.isNotEmpty(platformSearchVO.getMobile())) {
            queryWrapper.eq("mobile", platformSearchVO.getMobile());
        }
        //平台类型
        if (ObjectUtil.isNotEmpty(platformSearchVO.getType())) {
            queryWrapper.eq("type", platformSearchVO.getType());
        }
        //审核状态
        if (ObjectUtil.isNotEmpty(platformSearchVO.getStatus())) {
            queryWrapper.eq("status", platformSearchVO.getStatus());
        }

        IPage<Store> page1 = storeService.page(page, queryWrapper.lambda().orderByDesc(Store::getUpdateAt).orderByDesc(Store::getCreateAt));
        List<Store> records = page1.getRecords();
        if(ObjectUtil.isEmpty(records)){
            return voiPage.setRecords(storeListList);
        }
        voiPage.setCurrent(page1.getCurrent());
        voiPage.setSize(page1.getSize());
        voiPage.setPages(page1.getPages());
        voiPage.setTotal(page1.getTotal());

        records.forEach(store->{
            StoreListVO storeListVO = new StoreListVO();
            BeanUtils.copyProperties(store,storeListVO);
            storeListList.add(storeListVO);
        });

        List<Long> storeIds = storeListList.stream()
                .filter(g -> PlatformStatusEnum.REFUSE_REPEAT_AUDIT.status().equals(g.getStatus()) || PlatformStatusEnum.REFUSE_AUDIT.status().equals(g.getStatus()))
                .map(StoreListVO::getId)
                .collect(Collectors.toList());

        if(ObjectUtil.isEmpty(storeIds)){
            voiPage.setRecords(storeListList);
            return voiPage;
        }

        List<StoreAudit> storeAudits = storeAuditService.list(new QueryWrapper<StoreAudit>().lambda().in(StoreAudit::getStoreId, storeIds));
        for (StoreListVO store:storeListList){
            storeAudits.forEach(item->{
                if(store.getId().equals(item.getStoreId())){
                    store.setRejectReason(item.getRejectReason());
                }
            });
        }
        voiPage.setRecords(storeListList);
        return voiPage;
    }

    /**
     * 个体平台入驻详情信息
     * @param storeId
     * @return
     */
    public PlatformDetailsVO details(Long storeId,Boolean isDetails){
        //创建返回实体对象
        PlatformDetailsVO detailsVO = new PlatformDetailsVO();
        //查询个体平台入驻信息
        Store store = storeService.getById(storeId);
        if(ObjectUtil.isEmpty(store)){
            throw new PlatformException("个体平台入驻信息为空");
        }
        Integer type = store.getType();
        Idcard idcard = null;
        if(ObjectUtil.isNotEmpty(store.getIdcardNo())){
            idcard = idcardService.getOne(new QueryWrapper<Idcard>().eq("idcard_no", store.getIdcardNo()));
        }

        //如果类型为1，个人个体平台详情信息封装
        if(PlatformTypeEnum.PERSONAL_PLATFORM.type().equals(type)){
            detailsVO.setName(idcard == null?null:idcard.getName());
            detailsVO.setExpireDate(idcard == null?null:idcard.getExpireDate());
            detailsVO.setIdcardBack(idcard == null?null:idcard.getIdcardBack());
            detailsVO.setIdcardFront(idcard == null?null:idcard.getIdcardFront());
            detailsVO.setIdcardNo(idcard == null?null:idcard.getIdcardNo());
            detailsVO.setMobile(store.getMobile());
            detailsVO.setCreateAt(store.getCreateAt());
            detailsVO.setTitle(store.getTitle());
        }


        //如果类型为2，查询企业平台详情信息
        if(PlatformTypeEnum.ENTERPRISE_PLATFORM.type().equals(type)){
            //不需要复审详情信息
            detailsVO.setTitle(store.getTitle());
            detailsVO.setName(idcard == null?null:idcard.getName());
            detailsVO.setCreateAt(store.getCreateAt());
            detailsVO.setMobile(store.getMobile());
            //需要复审详情信息
            if(isDetails){
                //查询公司信息
                Company company = companyService.getOne(new QueryWrapper<Company>().eq("id", store.getCompanyId()));
                //查询公司省份证信息
                Idcard idcard1 = idcardService.getOne(new QueryWrapper<Idcard>().eq("idcard_no", company.getCorporationIdcard()));
                //封装返回数据
                detailsVO.setCompanyName(company.getName());
                detailsVO.setAddress(company.getAddress());
                detailsVO.setCorporation(company.getCorporation());
                detailsVO.setCorporationIdcard(company.getCorporationIdcard());
                detailsVO.setThreeInOne(company.getThreeInOne());
                detailsVO.setSocialCreditCode(company.getSocialCreditCode());
                detailsVO.setLicense(company.getLicense());
                detailsVO.setIdcardFront(idcard1.getIdcardFront());
                detailsVO.setIdcardBack(idcard1.getIdcardBack());
            }
        }

        detailsVO.setLogo(store.getLogo());

        return detailsVO;
    }

}
